package com.iretailer.report.service.report;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.iretailer.report.base.BaseAction;
import com.iretailer.report.model.*;
import com.iretailer.report.util.StringUtil;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.AsyncResult;
import org.springframework.stereotype.Component;

import java.util.*;
import java.util.concurrent.Future;
import java.util.stream.Collectors;

/**
 * @Auther: xjw
 * @Date: 2019/7/19 0019 11:01
 * @Description:
 */
@Component
public class UserControllerAsync extends BaseAction {

    @Async
    public Future<List<JSONObject>> assembleMalls(List<Malls> malls, List<String> mallIds) {

        List<PhysicalZoneMall> phys = physicalzonesService.selectPhysicalZoneZoneType(1, mallIds);
        Map<String, List<PhysicalZoneMall>> physMap = phys.stream().collect(Collectors.groupingBy(PhysicalZoneMall::getMallId));
        List<DeviceType> devices = devicesService.selectDeviceType(new QueryWrapper<DeviceType>().lambda().eq(DeviceType::getEnabled, 1));
        Map<String, List<DeviceType>> deviceMap = devices.stream().collect(Collectors.groupingBy(DeviceType::getMallId));
        Map<String, List<HeatChatRule>> mapHeatChatRule = heatChatRuleService.listHeatChatRule(mallIds).stream().collect(Collectors.groupingBy(HeatChatRule::getMallId));
        List<JSONObject> mallObject = new ArrayList<>();
        for (Malls m : malls) {
            JSONObject o = JSONObject.parseObject(JSON.toJSONString(m));
            List<PhysicalZoneMall> phy = physMap.get(m.getId());
            if (phy != null) {
                // 排序
                o.put("PhysicalZones", phy.stream().sorted(Comparator.comparing(PhysicalZoneMall::getRanked)).collect(Collectors.toList()));
            } else {
                o.put("PhysicalZones", Collections.emptyList());
            }
            List<DeviceType> device = deviceMap.get(m.getId());
            if (device != null) {
                Map<String, List<DeviceType>> deviceTypeNameMap = device.stream().collect(Collectors.groupingBy(DeviceType::getDeviceTypeName));
                o.put("Device", deviceTypeNameMap);
            }
            if (mapHeatChatRule != null) {
                o.put("HeatChatRule", mapHeatChatRule.get(m.getId()));
            }
//            if (easAlarm != null) {
//                //排序
//                o.put("EasAlarmDevice", easAlarm);
//            } else {
//                o.put("EasAlarmDevice", Collections.emptyList());
//            }
            mallObject.add(o);
        }
        return new AsyncResult<>(mallObject);
    }

    @Async
    public Future<List<MallTagType>> assembleTagType(List<String> mallIds) {
        List<MallTagType> mallTagTypes = tagTypeMallsService.selectMallTagTypeByMallIds(mallIds);
        Map<String, List<MallTagType>> mallTagTypeMap = mallTagTypes.stream().collect(Collectors.groupingBy(MallTagType::getId));
        List<MallTagType> mallObject = new ArrayList<>();
        if (mallTagTypeMap != null) {
            mallTagTypeMap.entrySet().forEach(it -> {
                MallTagType o = it.getValue().get(0);
                List<String> ms = new ArrayList<>();
                it.getValue().forEach(m -> {
                    ms.add(m.getMallId());
                });
                o.setMallId(null);
                o.setMallIds(ms);
                mallObject.add(o);
            });
        }
        // 排序
        return new AsyncResult<>(mallObject.stream().sorted(Comparator.comparing(MallTagType::getRanked)).collect(Collectors.toList()));
    }

    @Async
    public Future<List<Menus>> assembleMenus(User newUserInfo) {
        String roleId = newUserInfo.getRoleId();
        //查询出菜单
        //管理员
        List<Menus> menus = new ArrayList<>();
        if (StringUtil.notEmpty(roleId) && roleId.trim().equals("-")) {
            menus = menusService.selectMenusByVisible("1");
        } else {
            Role role = roleService.getById(roleId);
            menus = menusService.selectMenusByRoleId(role.getId());
        }
        List<Menus> menusTree = menusService.convertTree(menus, "-");
//        data.put("Menus", menusTree);
        return new AsyncResult<>(menusTree);
    }

    @Async
    public Future<String> showLogo(String companyId) {
        Companies companies = companyService.getById(companyId);
        String showLogo = "";
        if (companies != null) {
            showLogo = companies.getShowLogo();
        }
        return new AsyncResult<>(showLogo);
    }

    @Async
    public Future<List<PageWidgets>> pageWidgets(String userId) {
        List<PageWidgets> userPageWidgets = pageWidgetService.selectPageWidgetsByUserId(userId);
        return new AsyncResult<>(userPageWidgets);
    }
}
